package SmartHome.dao.impl;

import SmartHome.dao.DeviceTypeDao;
import SmartHome.entity.DeviceTypeInfo;
import SmartHome.util.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DeviceTypeImpl implements DeviceTypeDao{

    public int executeUpdate(String sql, Object... params) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            // 1.加载驱动
            // 2.连接数据库
            conn = JdbcUtil.getConn();
            // 3.创建语句
            ps = conn.prepareStatement(sql);
            // 遍历参数
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            // 4.执行语句
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 5.释放资源
            JdbcUtil.close(conn, ps, null);
        }
        return 0;
    }

    @Override
    public void add(DeviceTypeInfo DevicesType) {
        String sql = "insert into DeviceTypeInfo(DevicesTypeId,DevicesTypeName,dtState) values (?,?,1)";
        this.executeUpdate(sql, DevicesType.getDevicesTypeId(), DevicesType.getDevicesTypeName());
    }

    @Override
    public void delete(int DevicesTypeId) {
        String sql = "update DeviceTypeInfo set dtState=0 where DevicesTypeId = ?";
        this.executeUpdate(sql, DevicesTypeId);
    }

    @Override
    public DeviceTypeInfo query(int DevicesTypeId) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        String sql = "select * from DeviceTypeInfo where dtState=1 and DevicesTypeId = "+DevicesTypeId;
        try {
            // 1.加载驱动
            // 2.连接数据库
            conn = JdbcUtil.getConn();
            // 3.创建语
            st = conn.createStatement();
            // 4.执行语句
            rs = st.executeQuery(sql);
            if (rs.next()) {
                DeviceTypeInfo DevicesType = new DeviceTypeInfo(rs.getInt(1),rs.getString(2),1);
                return DevicesType;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 5.释放资源
            JdbcUtil.close(conn, st, rs);
        }
        return null;
    }

    @Override
    public void update(int DevicesTypeId, DeviceTypeInfo DevicesType) {
        String sql = "update DeviceTypeInfo set DevicesTypeId=?, DevicesTypeName=? where DevicesTypeId =? ";
        this.executeUpdate(sql, DevicesType.getDevicesTypeId(), DevicesType.getDevicesTypeName());
    }

    @Override
    public List<DeviceTypeInfo> getAll() {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;

        try {
            conn = JdbcUtil.getConn();
            st = conn.createStatement();
            String sql = "select * from DeviceTypeInfo where dtState=1 ";
            System.out.println(sql);
            rs = st.executeQuery(sql);
            List<DeviceTypeInfo> list = new ArrayList<DeviceTypeInfo>();

            while(rs.next()) {
                DeviceTypeInfo deviceTypeInfo = new DeviceTypeInfo(rs.getInt(1), rs.getString("DevicesTypeName"),1);
                list.add(deviceTypeInfo);
            }
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, st, rs);
        }
        return null;
    }
}
